<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>Audio Output Example - Record Plugin for Video.js</title>

  <link href="//unpkg.com/video.js@8.10.0/dist/video-js.min.css" rel="stylesheet">
  <link href="//unpkg.com/videojs-wavesurfer/dist/css/videojs.wavesurfer.min.css" rel="stylesheet">
  <link href="//unpkg.com/videojs-record/dist/css/videojs.record.css" rel="stylesheet">
  <link href="assets/examples.css" rel="stylesheet">

  <script src="//unpkg.com/video.js@8.10.0/dist/video.min.js"></script>
  <script src="//unpkg.com/recordrtc/RecordRTC.js"></script>
  <script src="//unpkg.com/webrtc-adapter/out/adapter.js"></script>
  <script src="//unpkg.com/wavesurfer.js@6.6.4/dist/wavesurfer.min.js"></script>
  <script src="//unpkg.com/wavesurfer.js@6.6.4/dist/plugin/wavesurfer.microphone.min.js"></script>
  <script src="//unpkg.com/videojs-wavesurfer/dist/videojs.wavesurfer.min.js"></script>

  <script src="//unpkg.com/videojs-record/dist/videojs.record.js"></script>

  <script src="browser-workarounds.js"></script>

  <style>
  body {
      font-weight: 300;
  }

  /* change player background color */
  #myAudio {
      background-color: #8EDBE6;
  }

  select {
      margin: 0 0 0.5em 0;
  }

  .outputSelector {
      margin-top: 30px;
      display: none;
  }
  </style>

</head>
<body>

<div>
  <audio id="myAudio" class="video-js vjs-default-skin"></audio>

  <div class="outputSelector">
    <label>Select audio output: </label>
    <select id="selector"></select>
  </div>
</div>

<script>
var devices, deviceId;
var options = {
    controls: true,
    bigPlayButton: false,
    width: 600,
    height: 300,
    fluid: false,
    plugins: {
        wavesurfer: {
            backend: 'WebAudio',
            waveColor: 'black',
            progressColor: '#2E732D',
            displayMilliseconds: true,
            debug: true,
            cursorWidth: 1,
            hideScrollbar: true,
            plugins: [
                // enable microphone plugin
                WaveSurfer.microphone.create({
                    bufferSize: 4096,
                    numberOfInputChannels: 1,
                    numberOfOutputChannels: 1,
                    constraints: {
                        video: false,
                        audio: true
                    }
                })
            ]
        },
        record: {
            audio: true,
            video: false,
            maxLength: 20,
            displayMilliseconds: true,
            debug: true
        }
    }
};
var outputSection = document.getElementsByClassName('outputSelector')[0];

// apply some workarounds for certain browsers
applyAudioWorkaround();

var player = videojs('myAudio', options, function() {
    // print version information at startup
    var msg = 'Using video.js ' + videojs.VERSION +
        ' with videojs-record ' + videojs.getPluginVersion('record') +
        ', videojs-wavesurfer ' + videojs.getPluginVersion('wavesurfer') +
        ', wavesurfer.js ' + WaveSurfer.VERSION + ' and recordrtc ' +
        RecordRTC.version;
    videojs.log(msg);

    // enumerate devices at startup
    player.record().enumerateDevices();
});

player.on('enumerateReady', function() {
    // show output selector section
    outputSection.style.display = 'block';

    devices = player.record().devices;

    // handle selection changes
    var outputSelector = document.getElementById('selector');
    outputSelector.addEventListener('change', changeAudioOutput);

    // populate select options
    var deviceInfo, option, i;
    for (i = 0; i !== devices.length; ++i) {
        deviceInfo = devices[i];
        option = document.createElement('option');
        option.value = deviceInfo.deviceId;
        if (deviceInfo.kind === 'audiooutput') {
            console.info('Found audio output device: ', deviceInfo.label);
            option.text = deviceInfo.label || 'speaker ' +
                (outputSelector.length + 1);
            outputSelector.appendChild(option);
        }
    }

    if (outputSelector.length == 0) {
        // no output devices found, disable select
        option = document.createElement('option');
        option.text = 'No audio output devices found';
        option.value = undefined;
        outputSelector.appendChild(option);
        outputSelector.disabled = true;
        console.warn(option.text);
    } else {
        console.info('Total output devices found:', outputSelector.length);
    }
});

function changeAudioOutput(event) {
    deviceId = event.target.value;

    try {
        // change audio output device
        player.record().setAudioOutput(deviceId);
    } catch (error) {
        console.warn(error);

        // jump back to first output device in the list as it's the default
        event.target.selectedIndex = 0;
    }
}

player.on('audioOutputReady', function() {
    console.log('Changed audio output to deviceId:', deviceId);
});

// error handling
player.on('enumerateError', function() {
    console.warn('enumerate error:', player.enumerateErrorCode);
});

player.on('deviceError', function() {
    console.warn('device error:', player.deviceErrorCode);
});

player.on('error', function(element, error) {
    console.error(error);
});

// user clicked the record button and started recording
player.on('startRecord', function() {
    console.log('started recording!');
});

// user completed recording and stream is available
player.on('finishRecord', function() {
    // the blob object contains the recorded data that
    // can be downloaded by the user, stored on server etc.
    console.log('finished recording: ', player.recordedData);
});
</script>

</body>
</html>
